package org.hxzon.cyclone.pfgen;

import org.hxzon.cyclone.Particle;
import org.hxzon.cyclone.Vector3;

//pfgen.h,pfgen.cpp
//阻力发生器
public class ParticleDrag implements ParticleForceGenerator {
    /** Holds the velocity drag coeffificent. */
    private double k1;

    /** Holds the velocity squared drag coeffificent. */
    private double k2;

    public ParticleDrag(double k1, double k2) {
        this.k1 = k1;
        this.k2 = k2;
    }

    public void updateForce(Particle particle, double duration) {
        Vector3 force = particle.getVelocity().copy();

        // 阻力计算公式
        double dragCoeff = force.magnitude();
        dragCoeff = k1 * dragCoeff + k2 * dragCoeff * dragCoeff;

        force.normalise();
        force.multiUpdate(-dragCoeff);
        particle.addForce(force);
    }
}
